﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.DataLayer;
using BaHuan.Entities;
using BaHuan.Common;
using BaHuan.BusinessLayer;

namespace BaHuan
{
    public partial class FrmMucChi : Form
    {
        MucChiMgt mucchiMgt = new MucChiMgt();
        int idEdit = -1;
        public FrmMucChi()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            txtTenMC.Enabled = true;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            idEdit = -1;
            txtTenMC.Text = "";
        }


        private void ReloadGridview()
        {
            dgvMucChi.AutoGenerateColumns = false;
            dgvMucChi.DataSource = null;
            dgvMucChi.DataSource = GetAllMucChi();
        }

        private void FrmMucChi_Load(object sender, EventArgs e)
        {
            txtTenMC.Enabled = false;
            ReloadGridview();
        }

        private void dgvMucChi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                btnLuu.Enabled = true;
                btnXoa.Enabled = true;
                btnSua.Enabled = true;
                idEdit = Convert.ToInt32(dgvMucChi.Rows[e.RowIndex].Cells[0].Value.ToString());
                txtTenMC.Text = dgvMucChi.Rows[e.RowIndex].Cells[1].Value.ToString();
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (idEdit == -1)
            {
                MucChi mc = new MucChi();
                mc.Ten = txtTenMC.Text;
                mucchiMgt.SaveNewMucChi(mc);
            }
            else
            {
                MucChi mc = new MucChi();
                mc.Ten = txtTenMC.Text;
                mc.Id = idEdit;
                mucchiMgt.UpdateMucChi(mc);
            }
            txtTenMC.Enabled = false;
            ReloadGridview();
        }

        private List<MucChi> GetAllMucChi()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = mucchiMgt.GetAllMucChi();
            List<MucChi> listMucChi = (List<MucChi>)resultObj.Result;

            return listMucChi;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            txtTenMC.Enabled = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (idEdit != -1)
            {
                DialogResult result;
                result = MessageBox.Show("Bạn có chắc chắn muốn xóa mục chi này không?", "Xóa mục chi", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    MucChi mc = new MucChi();
                    mc.Id = idEdit;
                    mc.Ten = txtTenMC.Text;
                    mucchiMgt.DeleteMucChi(mc);
                    ReloadGridview();
                    MessageBox.Show("Đã xóa", "Thông báo");
                }
            }
            else
            {
                MessageBox.Show("Mục chi này không tồn tại","Thông báo");
            }
        }
    }
}